# _*_ coding utf-8 _*_
# FILENAME：scheduleForm.py
# DESCRIPTION: 排班管理Form
# AUTH: 
# DATE: 2022/6/1 10:56 上午
from PyQt5.QtCore import QTime
from PyQt5.QtWidgets import *

from components.MessageBoxComponent import MessageBox
from components.SelectFactoryComponent import SelectFactoryComponent
from models.baseModel import get_session
from models.scheduleModel import ScheduleModel


class ScheduleForm(QDialog):

    __id = 0

    def __init__(self, parent=None, id=0):
        super(ScheduleForm, self).__init__(parent)
        self.init_ui(parent)
        title = "新增排班"
        self.__id = id
        if id > 0:
            title = "编辑排班"
            self.edit_data()
        self.setWindowTitle(title)

    def edit_data(self):
        session = get_session()
        data = session.query(ScheduleModel).filter(ScheduleModel.id == self.__id).filter(ScheduleModel.is_del == 0).all()
        session.close()
        if data:
            self.textEdit_name.setText(data[0].item)
            self.timeEdit_start.setTime(QTime.fromString(data[0].begin_time, 1))
            self.timeEdit_end.setTime(QTime.fromString(data[0].end_time, 1))
            self.order_type_combox.setCurrentText(data[0].order_type)
            self.work_combox.setCurrentText(data[0].work_type)
            self.fac_combox.selectedById(int(data[0].factory_id))

    def init_ui(self, parent):
        self.resize(300, 280)
        '''水平布局'''
        hbox = QHBoxLayout()

        self.save_btn = QPushButton()
        self.save_btn.setText('保存')
        self.save_btn.clicked.connect(lambda: self.save_btn_click(parent))

        self.cancel_btn = QPushButton()
        self.cancel_btn.setText('取消')
        self.cancel_btn.clicked.connect(self.cancel_btn_click)

        # 定义对象
        self.label_name = QLabel("时间项:")
        self.textEdit_name = QLineEdit()
        self.textEdit_name.setPlaceholderText('请输入时间项')

        self.label_start = QLabel("开始时间:")
        self.timeEdit_start = QDateTimeEdit(QTime.currentTime(), self)
        self.timeEdit_start.setDisplayFormat("HH:mm:ss")

        self.label_end = QLabel("结束时间:")
        self.timeEdit_end = QDateTimeEdit(QTime.currentTime(), self)
        self.timeEdit_end.setDisplayFormat("HH:mm:ss")

        self.fac_lab = QLabel()
        self.fac_lab.setText('所属工厂:')
        self.fac_combox = SelectFactoryComponent()
        self.fac_combox.setPlaceholderText('请选择工厂')
        # self.fac_combox.addItems(["工厂一|1", "工厂二|2", "工厂三|3", "工厂四|4"])
        # self.fac_combox.setCurrentIndex(0)

        self.order_type_labal = QLabel()
        self.order_type_labal.setText('排班类型:')
        self.order_type_combox = QComboBox()
        self.order_type_combox.addItems(["短白班", "长白班", "白夜班"])
        self.order_type_combox.setCurrentIndex(0)

        self.work_lab = QLabel()
        self.work_lab.setText('作息类型:')
        self.work_combox = QComboBox()
        self.work_combox.addItems(["上班时间", "休息时间"])
        self.work_combox.setCurrentIndex(0)
        #  布局
        gbox = QGridLayout()
        gbox.addWidget(self.label_name, 0, 0, 1, 1)
        gbox.addWidget(self.textEdit_name, 0, 1, 1, 2)

        gbox.addWidget(self.label_start, 1, 0, 1, 1)
        gbox.addWidget(self.timeEdit_start, 1, 1, 1, 2)

        gbox.addWidget(self.label_end, 2, 0, 1, 1)
        gbox.addWidget(self.timeEdit_end, 2, 1, 1, 2)

        gbox.addWidget(self.order_type_labal, 3, 0, 1, 1)
        gbox.addWidget(self.order_type_combox, 3, 1, 1, 2)

        gbox.addWidget(self.work_lab, 4, 0, 1, 1)
        gbox.addWidget(self.work_combox, 4, 1, 1, 2)

        gbox.addWidget(self.fac_lab, 5, 0, 1, 1)
        gbox.addWidget(self.fac_combox, 5, 1, 1, 2)

        gbox.addWidget(self.save_btn, 6, 1, 1, 1)
        gbox.addWidget(self.cancel_btn, 6, 2, 1, 1)

        vbox = QVBoxLayout()
        vbox.addLayout(gbox)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

    # 保存
    def save_btn_click(self, parent):
        if self.textEdit_name.text().strip() == '':
            messageBox = MessageBox()
            messageBox.warrning(title="提醒", msg="时间项不能为空")
            return

        session = get_session()
        if self.__id == 0:
            model = ScheduleModel()
            #  新增
            model.item = self.textEdit_name.text()
            model.factory_id = self.fac_combox.getId()
            model.begin_time = self.timeEdit_start.text()
            model.end_time = self.timeEdit_end.text()
            model.order_type = self.order_type_combox.currentText()
            model.work_type = self.work_combox.currentText()
            session.add(model)
        else:
            #  编辑
            params = {
                'item': self.textEdit_name.text(),
                'factory_id': self.fac_combox.getId(),
                'begin_time': self.timeEdit_start.text(),
                'end_time': self.timeEdit_end.text(),
                'order_type': self.order_type_combox.currentText(),
                'work_type': self.work_combox.currentText()
            }
            session.query(ScheduleModel).filter(ScheduleModel.id == self.__id).update(params)

        session.commit()
        session.close()

        # 刷新列表
        self.parent().query_click()
        self.close()

    def cancel_btn_click(self):
        self.close()

    @staticmethod
    def get_dialog(parent=None, id=0):
        dialog = ScheduleForm(parent, id)
        return dialog.exec()